var element = layui.element,layer = layui.layer
    ,table = layui.table
    ,$ = layui.jquery,deltargetgroupul,
    laydate = layui.laydate,form=layui.form,types = "";
var data = [{"targetgrouptext":"测试1"},{"targetgrouptext":"测试2"},{"targetgrouptext":"测试3"},{"targetgrouptext":"测试4"},{"targetgrouptext":"测试5"}];
var data1 = [{'id':'1','targettimeperiod':'text1','dollartargetamount':'text2'},{'id':'2','targettimeperiod':'text1','dollartargetamount':'text2'},{'id':'3','targettimeperiod':'text1','dollartargetamount':'text2'},{'id':'4','targettimeperiod':'text1','dollartargetamount':'text2'}];

lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

//动态生成element
var targetgroupmenuulhtml = "";
for (var i = 0;i<data.length;i++){
    targetgroupmenuulhtml += '<li class="layui-nav-item"><a href="javascript:void(0)">'+data[i].targetgrouptext+'</a></li>';
}
$("#targetgroupnav").html(targetgroupmenuulhtml);
element.render();

$("#searchs").on("click",function () {
    var personnel = $('#personnel').val();
    layer.msg("搜索"+personnel);
    //根据结果动态生成
    var targetgroupmenuulhtml = "";
    for (var i = 0;i<data.length;i++){
        targetgroupmenuulhtml += '<li class="layui-nav-item"><a href="javascript:void(0)">'+data[i].targetgrouptext+'</a></li>';
    }
    $("#targetgroupnav").html(targetgroupmenuulhtml);
    element.render();
})

//监听导航点击
element.on('nav(targetgroupnav)', function(elem){
    //console.log(elem)
    layer.msg(elem.text());
    deltargetgroupul = elem.text();
    table.reload('targettableid', {
        data:data1,
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: {deltargetgroupul:deltargetgroupul}  //表格查询条件
    }, 'data'); //表格刷新
    return false;
});

table.render({
    elem: '#targetid'
    ,data:data1
    ,toolbar: '#toolbarDemo'
    ,cols: [[
        {checkbox: true, width:50, fixed: true}
        ,{field:'targettimeperiod', title:'目标时间段', fixed: true}
        ,{field:'dollartargetamount', title:'美元目标金额', fixed: true}
        ,{fixed: 'right', width:180, align:'center', toolbar: '#targeteditor', title:'操作'}
    ]]
    ,id: 'targettableid'
    ,height: $(document).height() - $('#targetid').offset().top - 50
    ,page: true
});
table.on('toolbar(targettable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
        case 'newtarget':
            $('#targetUpdateform')[0].reset();
            form.render();
            layer.open({
                type: 1,
                title: "新增目标",
                area: ['685px', '230px'],
                content: $("#targetUpdate")//引用的弹出层的页面层的方式加载修改界面表单
            });
            types = 0;
            break;
        case 'deltarget':
            var data = checkStatus.data;
            var delid = [];
            for (var i = 0;i<data.length;i++){
                delid.push(data[i].id)
            }
            layer.msg('需要删除的数据ID为：'+ JSON.stringify(delid) + ',执行删除');
            table.reload(); //ajax删除成功之后刷新表格
            break;
    };
});

//监听工具条
table.on('tool(targettable)', function(obj){
    var data = obj.data;
    if(obj.event === 'edit'){
        $('#targetUpdateform')[0].reset();
        form.render();
        layer.open({
            type: 1,
            title: "修改目标",
            area: ['685px', '230px'],
            content: $("#targetUpdate")//引用的弹出层的页面层的方式加载修改界面表单
        });
        types = 1;
    }
});

form.on('submit(saves)', function(data){
    layer.msg(JSON.stringify(data.field));
    if(types==0){
        //新增操作
    }else{
        //修改操作
    }
    table.reload('targettableid', {
        data:data1,
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: {deltargetgroupul:deltargetgroupul}  //表格查询条件
    }, 'data'); //表格刷新
    layer.msg("保存成功", {icon: 6});
    setTimeout(function(){
        layer.closeAll();//关闭所有的弹出层
    }, 1000);
    return false;
});
